class user():
    def __init__(self):
        self.user_information = {'root':{'passwd':'123456','budge':'100000000'}}

    def shopping_items(self):
        global item_list,store_item
        item_list = {'apple':{'price':3,'inventory':10},'orange':{'price':2,'inventory':19}}
        print("商品清单")
        for k, v in item_list.items():
            store_item.append(k)
            print(f"商品名称：{k},商品价格：{item_list[k].get('price')}元/斤,商品库存：{item_list[k].get('inventory')}斤")
    def shopping(self,name):
        self.shopping_items()
        item_name = input("请输入你要购买的商品名称：")
        item_count = input("请输入你要购买的商品数量：")
        if  store_item in store_item or not(item_count.isnumeric()):
            print("你的输入有误，请重新输入")
            self.shopping(name)
        item_count = int(item_count)
        if item_count > int(item_list[item_name].get('inventory')):
            print("商品库存不够，请重新输入")
            self.shopping(name)
        elif int(int(item_list[item_name].get('price'))*item_count) > int(self.user_information[name].get('budge')):
            print("你的余额不足请重新输入")
            self.shopping(name)
        else:
            self.pay(name,int(int(item_list[item_name].get('price'))*item_count))
            self.shopping_list(name,item_name,item_count)

    def pay(self,name,price):
        self.user_information[name]['budge'] = int(self.user_information[name]['budge']) - price
        print("恭喜你购买成功")
    def shopping_list(self,name,i_name,count):
        pass

class Login():
    def __init__(self):
        self.user1 = user()
    def user_login(self):
        name = input("请输入你的用户名：")
        passwd = input("请输入你的密码：")
        self.find_user(name,passwd)
        print("登陆成功，现在开始愉快的购物吧")
        self.user1.shopping(name)

    def creat_new_account(self):
        name = input("请输入你要注册的用户名：")
        passwd = input("请输入你的密码：")
        budge = input("请输入你的预算：")
        self.user1.user_information[name] = {'passwd':passwd,'budge':budge}
        print('注册成功，现在开始愉快的购物吧')
        self.user1.shopping(name)

    def find_user(self, name, passwd):
        if self.user1.user_information == {}:
            return False
        else:
            i = 0
            while i < 3:
                try:
                    if name in self.user1.user_information:
                        if self.user1.user_information[name].get('passwd') == passwd:
                            return True
                        else:
                            passwd = input("密码输入错误，请重新输入:")
                    else:
                        name =input("用户名输入错误，请重新输入:")
                        passwd = input("请输入密码:")
                    i = i + 1
                except:
                    pass
        print("登陆成功,祝你购物愉快")


start = Login()
print("欢迎来到三乐购物中心")
print("请选择你要进行的操作")
store_item = []
item_list={}
while True:
    do_what = input("登陆请输入：1，注册请输入：2，退出请输入：3 :")
    if do_what == '1':
        start.user_login()
        break
    elif do_what == '2':
        start.creat_new_account()
        break
    elif do_what == '3':
        exit()
    else:
        print("您的输入有误，请重新输入")
